<?php

/**
 *字符串相关
 * @wfs: 2020/10/16
 */

namespace base\other;

// use base\other\Actions;
// \base\other\Actions::getMethodDesc();
// 获取方法注释  	 Actions::getMethodDesc();		

class Actions
{

	/* 
	*获取方法注释 
	* @return string  2106071808016498
	*/
	public static function getMethodDesc($keys = '')
	{
		$data = [
			'global login' => ''
		];

		try {

			$request = request();
			$controller = explode("/", $request->pathinfo());
			$root = str_replace("/index.php", "", $request->root());
			$act = 'app' . '\\' . str_replace("/", "", $root) . '\controller';
			$action = '';
			foreach ($controller as $key => $val) {
				if ($key == count($controller) - 1) {
					$action = $val;
				} else if ($key == count($controller) - 2) {
					$act = $act . '\\' . ucwords(lcfirst($val));
				} else {
					$act = $act . '\\' . $val;
				};
			}
			$act = str_replace(".", "\\", $act);
			$func  = new \ReflectionMethod($act, $action);
			$tmp   = $func->getDocComment();

			//1.
			preg_match_all('/(.*?)\n/', $tmp, $arrs);
			foreach ($arrs[0] as $key => $val) {
				if (strstr($val, "Apidoc\Method")) {
					$start = strpos($val, '("') + 2;
					$end = strpos($val, '")') - $start;
					$data['method'] =  $d = substr($val, $start, $end);
				}
			}
			//2.
			foreach ($data as $key => $val) {
				preg_match_all('/@' . $key . '(.*?)\n/', $tmp, $code);
				$code = trim($code[1] ? $code[1][0] : '');
				$data[$key] = $code != '' ? $code : $data[$key];
			}
		} catch (\Exception $e) {
			$data['global login'] = 'true';
		}

		return  $keys ? $data[$keys] : $data;
	}
}
